package com.dao.impl;

import com.dao.RoleDao;
import com.pojo.Resources;
import com.pojo.Role;
import com.pojo.User;
import com.util.BaseJdbc;
import com.vo.ResourceId;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Role> getRoleAll(int page, int limit) {
        List<Role> list = new ArrayList<Role>();
        try {
            String sql="select * from ly_role limit ?,?";
            Object[] objects = new Object[2];
            objects[0]=page;
            objects[1]=limit;
            ResultSet resultSet = BaseJdbc.BaseSelect(sql, objects);
            while (resultSet.next()){
                Role role = new Role();
                role.setId(resultSet.getInt("id"));
                role.setName(resultSet.getString("name"));
                role.setStatus(resultSet.getInt("state"));
                role.setDesc(resultSet.getString("description"));
                role.setRoleKey(resultSet.getString("roleKey"));
                list.add(role);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Role> getRoleAll() {
        List<Role> list = new ArrayList<Role>();
        try {
            String sql="select * from ly_role";
            Object[] objects = new Object[1];
            ResultSet resultSet = BaseJdbc.BaseSelect(sql, objects);
            while (resultSet.next()){
                Role role = new Role();
                role.setId(resultSet.getInt("id"));
                role.setName(resultSet.getString("name"));
                role.setStatus(resultSet.getInt("state"));
                role.setDesc(resultSet.getString("description"));
                list.add(role);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ResourceId> getResourceIdAll(int roleid) {
        List<ResourceId> list = new ArrayList<ResourceId>();
        try {
            String sql="SELECT * FROM `ly_role_res` WHERE roleid=?";
            Object[] objects = new Object[1];
            objects[0]=roleid;
            ResultSet resultSet = BaseJdbc.BaseSelect(sql, objects);
            while (resultSet.next()){
                ResourceId resourceId = new ResourceId();
                resourceId.setId(resultSet.getInt("resid"));
                list.add(resourceId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteMenuByRoleId(int roleid) {
        int i =0;
        try {
           String sql="delete from ly_role_res where roleid=? ";
           Object[] objects = new Object[1];
           objects[0]=roleid;
           i =BaseJdbc.BaseUpdate(sql, objects);
       }catch (Exception e){
           e.printStackTrace();
       }
        return i;
    }

    @Override
    public int insertMenuByRoleId(int roleid, int mid) {
        int i =0;
        try {
            String sql="INSERT INTO ly_role_res(resid,roleid) VALUE(?,?)";
            Object[] objects = new Object[2];
            objects[0]=mid;
            objects[1]=roleid;
            i =BaseJdbc.BaseUpdate(sql, objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
}
